﻿using AutoMapper;
using ChineseAreaService;
using Sino.CapacityCloud.Repositories.EventData;
using Sino.CapacityCloud.Repositories.Models;
using Sino.CapacityCloud.WebApi.ApiControllers;
using Sino.Tms.Primitives;
using Sino.TMSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Sino.CapacityCloud.WebApi.Utilities
{

    public class AutoMapperProfile
        : Profile
    {

        private readonly IChinaAreaService _areaService;

        public AutoMapperProfile()
        {
            this._areaService = new ChinaAreaService();
        }

        protected override void Configure()
        {
            base.Configure();

            this.AddEnumConverter<CarLength>();
            this.AddEnumConverter<VehicleType>();
            this.AddEnumConverter<PriceUnit>();
            this.AddEnumConverter<GoodsUnit>();
            this.AddEnumConverter<MyOrderState>();
            this.AddEnumConverter<OrderSuppliedState>();
            this.AddEnumConverter<OrderGrabbedState>();
            this.AddEnumConverter<OrderType>();

            //Mapper.CreateMap<OrderStatus, MyOrderState>()
            //    .ConvertUsing(x => (MyOrderState)(
            //        (((int)x) > (int)OrderStatus.RTTO) ?
            //            (((int)x) - (int)OrderStatus.RTTO) :
            //            (int)MyOrderState.Waiting));
            Mapper.CreateMap<OrderStatus, MyOrderState>()
                .ConvertUsing(x => (new Func<MyOrderState>(() =>
                {
                    switch (x)
                    {
                        case OrderStatus.ITC:
                        case OrderStatus.WFTD:
                        case OrderStatus.BTS:
                        case OrderStatus.HSC:
                            return MyOrderState.Waiting;
                        case OrderStatus.SHIP:
                            return MyOrderState.Shipping;
                        case OrderStatus.TCHBD:
                        case OrderStatus.OEND:
                            return MyOrderState.Completed;
                        case OrderStatus.RTTO:
                        default:
                            return MyOrderState.None;
                    }
                }))());

            Mapper.CreateMap<EvaluateListItemVM, Repositories.Models.RatingRecord>()
                .ForMember(x => x.RatingRecordId, x => x.MapFrom(z => z.Id))
                .ForMember(x => x.CarrierOrderId, x => x.MapFrom(z => (string)null))
                .ForMember(x => x.OrderId, x => x.MapFrom(z => (string)null))
                .ForMember(x => x.CarId, x => x.MapFrom(z => (string)null))
                .ForMember(x => x.Attitude, x => x.MapFrom(z => z.Attitude))
                .ForMember(x => x.Timeliness, x => x.MapFrom(z => z.Timeliness))
                .ForMember(x => x.Pricing, x => x.MapFrom(z => z.Freight))
                .ForMember(x => x.Integrity, x => x.MapFrom(z => z.Credibility))
                .ForMember(x => x.Evaluation, x => x.MapFrom(z => z.Content))
                .ForMember(x => x.Time, x => x.MapFrom(z => SystemUtils.Try(() => DateTime.Parse(z.CreationTime), default(DateTime))));

            Mapper.CreateMap<FeeListItemVM, Repositories.Models.PayableCost>()
                .ForMember(x => x.PayableCostId, x => x.MapFrom(z => z.Id))
                .ForMember(x => x.Price, x => x.MapFrom(z => z.FeeAmount))
                .ForMember(x => x.CostType, x => x.MapFrom(z => z.FeeType))
                .ForMember(x => x.Progress, x => x.MapFrom(z => z.FeeStatus))
                .ForMember(x => x.PaymentMethod, x => x.MapFrom(z => z.PaymentMethod))
                .ForMember(x => x.Remarks, x => x.MapFrom(z => z.Comment))
                .ForMember(x => x.DrawCashStatus, x => x.MapFrom(z => z.CostDrawableStatus))
                .ForMember(x => x.SettleType, x => x.MapFrom(z => z.FeeType.IsFeeSingle()))
                .ForMember(x => x.CarrierOrderId, x => x.MapFrom(z => z.CarrierOrderId))
                .ForMember(x => x.PaymentDate, x => x.MapFrom(z => z.PaidDate ?? DateTime.Now));

            Mapper.CreateMap<FeeListItemVM, Cost>()
               .ForMember(x => x.Id, x => x.MapFrom(z => z.Id))
               .ForMember(x => x.SetttleBillId, x => x.MapFrom(z => z.FeeSetttleBillId))
               .ForMember(x => x.SetttleBillCode, x => x.MapFrom(z => z.FeeSettleBillCode))
               .ForMember(x => x.OrderId, x => x.MapFrom(z => z.CarrierOrderId))
               .ForMember(x => x.PaymentDate, x => x.MapFrom(z => z.PaidDate))
               .ForMember(x => x.LastPaymentTime, x => x.MapFrom(z => (DateTime?)null))
               .ForMember(x => x.Price, x => x.MapFrom(z => z.FeeAmount))
               .ForMember(x => x.Type, x => x.MapFrom(z => z.FeeType))
               .ForMember(x => x.SettleType, x => x.MapFrom(z => z.FeeType.IsFeeSingle()))
               .ForMember(x => x.PaymentMethod, x => x.MapFrom(z => z.PaymentMethod))
               .ForMember(x => x.Progress, x => x.MapFrom(z => z.FeeStatus))
               .ForMember(x => x.DrawCashStatus, x => x.MapFrom(z => z.CostDrawableStatus))
               .ForMember(x => x.Comment, x => x.MapFrom(z => z.Comment))
               .ForMember(x => x.BankName, x => x.MapFrom(z => z.OpenBank))
               .ForMember(x => x.BankCardNumber, x => x.MapFrom(z => z.BankCode))
               .ForMember(x => x.BankCardHolder, x => x.MapFrom(z => z.Holder));

            Mapper.CreateMap<OrderListItemVM, Repositories.Models.TransportLinkage>()
                .ForMember(x => x.StartingPlaceCode, x => x.MapFrom(z => z.OriginCode))
                .ForMember(x => x.StartingPlaceName, x => x.MapFrom(z => this._areaService.GetAreaName(z.OriginCode, true)))
                .ForMember(x => x.StartingPlaceLocation, x => x.MapFrom(z => new Point()))
                .ForMember(x => x.DestinationPlaceCode, x => x.MapFrom(z => z.DestinationCode))
                .ForMember(x => x.DestinationPlaceName, x => x.MapFrom(z => this._areaService.GetAreaName(z.DestinationCode, true)))
                .ForMember(x => x.DestinationPlaceLocation, x => x.MapFrom(z => new Point()));

            Mapper.CreateMap<OrderListItemVM, Line>()
                .ForMember(x => x.StartingPlaceCode, x => x.MapFrom(z => z.OriginCode))
                .ForMember(x => x.StartingPlaceName, x => x.MapFrom(z => this._areaService.GetAreaName(z.OriginCode, true)))
                .ForMember(x => x.StartingPlaceLatitude, x => x.MapFrom(z => 0))
                .ForMember(x => x.StartingPlaceLongitude, x => x.MapFrom(z => 0))
                .ForMember(x => x.DestinationCode, x => x.MapFrom(z => z.DestinationCode))
                .ForMember(x => x.DestinationName, x => x.MapFrom(z => this._areaService.GetAreaName(z.DestinationCode, true)))
                .ForMember(x => x.DestinationLatitude, x => x.MapFrom(z => 0))
                .ForMember(x => x.DestinationLongitude, x => x.MapFrom(z => 0));

            Mapper.CreateMap<OrderListItemVM, Repositories.Models.Order>()
                .ForMember(x => x.OrderId, x => x.MapFrom(z => z.Id))
                .ForMember(x => x.OrderNumber, x => x.MapFrom(z => z.OrderNumber))
                .ForMember(x => x.Amount, x => x.MapFrom(z => z.QuantityOfGoods))
                .ForMember(x => x.Category, x => x.MapFrom(z => z.GoodsTypeName))
                .ForMember(x => x.AmountUnit, x => x.MapFrom(z => z.GoodsUnit))
                .ForMember(x => x.CarCode, x => x.MapFrom(z => z.CarCode))
                .ForMember(x => x.CarLength, x => x.MapFrom(z => z.CarLength))
                .ForMember(x => x.DeliveryTime, x => x.MapFrom(z => z.DeliveryTime))
                .ForMember(x => x.ArrivalTime, x => x.MapFrom(z => z.ArrivalTime))
                .ForMember(x => x.State, x => x.MapFrom(z => z.OrderStatus))
                .ForMember(x => x.Remarks, x => x.MapFrom(z => z.Remarks))
                .ForMember(x => x.ContactName, x => x.MapFrom(z => z.Dispatcher))
                .ForMember(x => x.ContactChatId, x => x.MapFrom(z => z.DispatcherId.ToString()))
                .ForMember(x => x.ContactPhoneNumber, x => x.MapFrom(z => z.DispatcherPhone))
                .ForMember(x => x.Type, x => x.MapFrom(z => OrderType.Bulk))
                .ForMember(x => x.TransportLinkage, x => x.MapFrom(z => z));

            Mapper.CreateMap<OrderListItemVM, Repositories.Models.ConfirmedOrder>()
                .ForMember(x => x.ConfirmedOrderId, x => x.MapFrom(z => z.Id))
                .ForMember(x => x.CarrierOrderId, x => x.MapFrom(z => z.CarrierOrderId))
                .ForMember(x => x.OrderNumber, x => x.MapFrom(z => z.OrderNumber))
                .ForMember(x => x.Amount, x => x.MapFrom(z => z.QuantityOfGoods))
                .ForMember(x => x.Category, x => x.MapFrom(z => z.GoodsTypeName))
                .ForMember(x => x.AmountUnit, x => x.MapFrom(z => z.GoodsUnit))
                .ForMember(x => x.CarCode, x => x.MapFrom(z => z.CarCode))
                .ForMember(x => x.CarLength, x => x.MapFrom(z => z.CarLength))
                .ForMember(x => x.DeliveryTime, x => x.MapFrom(z => z.DeliveryTime))
                .ForMember(x => x.ArrivalTime, x => x.MapFrom(z => z.ArrivalTime))
                .ForMember(x => x.State, x => x.MapFrom(z => z.OrderStatus))
                .ForMember(x => x.Remarks, x => x.MapFrom(z => z.Remarks))
                .ForMember(x => x.ContactName, x => x.MapFrom(z => z.Dispatcher))
                .ForMember(x => x.ContactChatId, x => x.MapFrom(z => z.DispatcherId.ToString()))
                .ForMember(x => x.ContactPhoneNumber, x => x.MapFrom(z => z.DispatcherPhone))
                .ForMember(x => x.DrawableCostCount, x => x.MapFrom(z => z.DrawableCostCount))
                .ForMember(x => x.Type, x => x.MapFrom(z => OrderType.Suborder))
                .ForMember(x => x.TransportLinkage, x => x.MapFrom(z => z));

            Mapper.CreateMap<OrderListItemVM, Order>()
                .ForMember(x => x.Id, x => x.MapFrom(z => z.CarrierOrderId))
                .ForMember(x => x.OrderNumber, x => x.MapFrom(z => z.OrderNumber))
                .ForMember(x => x.Amount, x => x.MapFrom(z => z.QuantityOfGoods))
                .ForMember(x => x.Category, x => x.MapFrom(z => z.GoodsTypeName))
                .ForMember(x => x.AmountUnit, x => x.MapFrom(z => z.GoodsUnit))
                .ForMember(x => x.CarLength, x => x.MapFrom(z => z.CarLength))
                .ForMember(x => x.DeliveryTime, x => x.MapFrom(z => z.DeliveryTime))
                .ForMember(x => x.ArrivalTime, x => x.MapFrom(z => z.ArrivalTime))
                .ForMember(x => x.MyOrderState, x => x.MapFrom(z => z.OrderStatus))
                .ForMember(x => x.Comment, x => x.MapFrom(z => z.Remarks))
                .ForMember(x => x.Contacts, x => x.MapFrom(z => z.Dispatcher))
                .ForMember(x => x.ContactsChatId, x => x.MapFrom(z => z.DispatcherId.ToString()))
                .ForMember(x => x.Telephone, x => x.MapFrom(z => z.DispatcherPhone))
                .ForMember(x => x.DrawableCostCount, x => x.MapFrom(z => z.DrawableCostCount))
                .ForMember(x => x.Type, x => x.MapFrom(z => OrderType.Suborder))
                .ForMember(x => x.Line, x => x.MapFrom(z => z));

            Mapper.CreateMap<ConsigneeListItemVM, Consignee>()
                .ForMember(x => x.ChildId, x => x.MapFrom(z => z.ChildId))
                .ForMember(x => x.Destination, x => x.MapFrom(z => z.Destination))
                .ForMember(x => x.Name, x => x.MapFrom(z => z.Name))
                .ForMember(x => x.PhoneNumber, x => x.MapFrom(z => z.PhoneNumber));

            Mapper.CreateMap<ReceiptAttachmentItemVM, ReceiptAttachment>()
               .ForMember(x => x.Id, x => x.MapFrom(z => z.Id))
               .ForMember(x => x.Name, x => x.MapFrom(z => z.Name))
               .ForMember(x => x.Path, x => x.MapFrom(z => z.Path));

            Mapper.CreateMap<OrderChildLineItemVM, ChildLineItem>()
               .ForMember(x => x.Id, x => x.MapFrom(z => z.Id))
               .ForMember(x => x.Destination, x => x.MapFrom(z => z.Destination))
               .ForMember(x => x.AttachmentList, x => x.MapFrom(z => z.AttachmentList));

            Mapper.CreateMap<Repositories.Models.TransportLinkage, Sino.CapacityCloud.WebApi.Line>()
                .ForMember(x => x.Id, x => x.MapFrom(z => z.TransportLinkageId))
                .ForMember(x => x.StartingPlaceCode, x => x.MapFrom(z => z.StartingPlaceCode))
                .ForMember(x => x.StartingPlaceName, x => x.MapFrom(z => z.StartingPlaceName))
                .ForMember(x => x.StartingPlaceLatitude, x => x.MapFrom(z => z.StartingPlaceLocationLatitude))
                .ForMember(x => x.StartingPlaceLongitude, x => x.MapFrom(z => z.StartingPlaceLocationLongitude))
                .ForMember(x => x.DestinationCode, x => x.MapFrom(z => z.DestinationPlaceCode))
                .ForMember(x => x.DestinationName, x => x.MapFrom(z => z.DestinationPlaceName))
                .ForMember(x => x.DestinationLatitude, x => x.MapFrom(z => z.DestinationLocationnLatitude))
                .ForMember(x => x.DestinationLongitude, x => x.MapFrom(z => z.DestinationLocationLongitude));

            Mapper.CreateMap<Line, Repositories.Models.TransportLinkage>()
                .ForMember(x => x.TransportLinkageId, x => x.MapFrom(z => z.Id))
                .ForMember(x => x.DestinationPlaceCode, x => x.MapFrom(z => z.DestinationCode))
                .ForMember(x => x.DestinationLocationnLatitude, x => x.MapFrom(z => z.DestinationLatitude))
                .ForMember(x => x.DestinationLocationLongitude, x => x.MapFrom(z => z.DestinationLongitude))
                .ForMember(x => x.DestinationPlaceName, x => x.MapFrom(z => z.DestinationName))
                .ForMember(x => x.StartingPlaceCode, x => x.MapFrom(z => z.StartingPlaceCode))
                .ForMember(x => x.StartingPlaceLocationLatitude, x => x.MapFrom(z => z.StartingPlaceLatitude))
                .ForMember(x => x.StartingPlaceName, x => x.MapFrom(z => z.StartingPlaceName));

            Mapper.CreateMap<Repositories.Models.CommonTransportLinkage, Line>()
                .ForMember(x => x.OperationTime, x => x.MapFrom(z => z.OperationTime))
                .ForMember(x => x.StartingPlaceCode, x => x.MapFrom(z => z.TransportLinkage.StartingPlaceCode))
                .ForMember(x => x.StartingPlaceName, x => x.MapFrom(z => SystemUtils.Try(() => this._areaService.GetAreaName(z.TransportLinkage.StartingPlaceCode, false), null)))
                .ForMember(x => x.StartingPlaceLatitude, x => x.MapFrom(z => 0))
                .ForMember(x => x.StartingPlaceLongitude, x => x.MapFrom(z => 0))
                .ForMember(x => x.DestinationCode, x => x.MapFrom(z => z.TransportLinkage.DestinationPlaceCode))
                .ForMember(x => x.DestinationName, x => x.MapFrom(z => SystemUtils.Try(() => this._areaService.GetAreaName(z.TransportLinkage.DestinationPlaceCode, false), null)))
                .ForMember(x => x.DestinationLatitude, x => x.MapFrom(z => 0))
                .ForMember(x => x.DestinationLongitude, x => x.MapFrom(z => 0));

            Mapper.CreateMap<Repositories.Models.FavouriteTransportLinkage, Line>()
                .ForMember(x => x.OperationTime, x => x.MapFrom(z => z.OperationTime))
                .ForMember(x => x.StartingPlaceCode, x => x.MapFrom(z => z.TransportLinkage.StartingPlaceCode))
                .ForMember(x => x.StartingPlaceName, x => x.MapFrom(z => SystemUtils.Try(() => this._areaService.GetAreaName(z.TransportLinkage.StartingPlaceCode, false), null)))
                .ForMember(x => x.StartingPlaceLatitude, x => x.MapFrom(z => 0))
                .ForMember(x => x.StartingPlaceLongitude, x => x.MapFrom(z => 0))
                .ForMember(x => x.DestinationCode, x => x.MapFrom(z => z.TransportLinkage.DestinationPlaceCode))
                .ForMember(x => x.DestinationName, x => x.MapFrom(z => SystemUtils.Try(() => this._areaService.GetAreaName(z.TransportLinkage.DestinationPlaceCode, false), null)))
                .ForMember(x => x.DestinationLatitude, x => x.MapFrom(z => 0))
                .ForMember(x => x.DestinationLongitude, x => x.MapFrom(z => 0));

            Mapper.CreateMap<Repositories.Models.Order, Sino.CapacityCloud.WebApi.Order>()
               // Id
               .ForMember(x => x.Id, x => x.MapFrom(z => z.CarrierOrderId ?? z.OrderId))
               .ForMember(x => x.OrderNumber, x => x.MapFrom(z => z.OrderNumber))
               // Price
               .ForMember(x => x.Price, x => x.MapFrom(z => z.Price))
               .ForMember(x => x.Amount, x => x.MapFrom(z => z.Amount))
               .ForMember(x => x.AmountUnit, x => x.MapFrom(z => z.AmountUnit))
               .ForMember(x => x.Category, x => x.MapFrom(z => z.Category))
               // Time
               .ForMember(x => x.ArrivalTime, x => x.MapFrom(z => z.ArrivalTime))
               .ForMember(x => x.DeliveryTime, x => x.MapFrom(z => z.DeliveryTime))
               // Car
               .ForMember(x => x.CarLength, x => x.MapFrom(z => z.CarLength))
               // Comments
               .ForMember(x => x.Comment, x => x.MapFrom(z => z.Remarks))
               .ForMember(x => x.GrabComment, x => x.MapFrom(z => z.GrabComment))
               // Status
               .ForMember(x => x.MyOrderState, x => x.MapFrom(z => z.State))
               .ForMember(x => x.OrderSuppliedState, x => x.MapFrom(z => OrderSuppliedState.None))
               .ForMember(x => x.Type, x => x.MapFrom(z => z.Type))
               .ForMember(x => x.IsFocus, x => x.MapFrom(z => z.IsFocus))
               // Dispacher
               .ForMember(x => x.Telephone, x => x.MapFrom(z => z.ContactPhoneNumber))
               .ForMember(x => x.Contacts, x => x.MapFrom(z => z.ContactName))
               .ForMember(x => x.ContactsChatId, x => x.MapFrom(z => z.ContactChatId))
               // Line
               .ForMember(x => x.Line, x => x.MapFrom(z => z.TransportLinkage));

            Mapper.CreateMap<Repositories.Models.ConfirmedOrder, Sino.CapacityCloud.WebApi.Order>()
               // Id
               .ForMember(x => x.Id, x => x.MapFrom(z => z.CarrierOrderId))
               .ForMember(x => x.OrderNumber, x => x.MapFrom(z => z.OrderNumber))
               // Price
               .ForMember(x => x.Price, x => x.MapFrom(z => z.Price))
               .ForMember(x => x.Amount, x => x.MapFrom(z => z.Amount))
               .ForMember(x => x.AmountUnit, x => x.MapFrom(z => z.AmountUnit))
               .ForMember(x => x.Category, x => x.MapFrom(z => z.Category))
               // Time
               .ForMember(x => x.ArrivalTime, x => x.MapFrom(z => z.ArrivalTime))
               .ForMember(x => x.DeliveryTime, x => x.MapFrom(z => z.DeliveryTime))
               // Car
               .ForMember(x => x.CarLength, x => x.MapFrom(z => z.CarLength))
               // Comments
               .ForMember(x => x.Comment, x => x.MapFrom(z => z.Remarks))
               .ForMember(x => x.GrabComment, x => x.MapFrom(z => z.GrabComment))
               // Status
               .ForMember(x => x.MyOrderState, x => x.MapFrom(z => z.State))
               .ForMember(x => x.OrderSuppliedState, x => x.MapFrom(z => OrderSuppliedState.None))
               .ForMember(x => x.Type, x => x.MapFrom(z => z.Type))
               .ForMember(x => x.IsFocus, x => x.MapFrom(z => z.IsFocus))
               .ForMember(x => x.DrawableCostCount, x => x.MapFrom(z => z.DrawableCostCount))
               .ForMember(x => x.CanDrawCash, x => x.MapFrom(z => z.DrawableCostCount > 0))
               // Dispacher
               .ForMember(x => x.Telephone, x => x.MapFrom(z => z.ContactPhoneNumber))
               .ForMember(x => x.Contacts, x => x.MapFrom(z => z.ContactName))
               .ForMember(x => x.ContactsChatId, x => x.MapFrom(z => z.ContactChatId))
               // Line
               .ForMember(x => x.Line, x => x.MapFrom(z => z.TransportLinkage));

            Mapper.CreateMap<Sino.CapacityCloud.Repositories.Models.Quotation, Sino.CommonServices.EventTypes.GrabbedOrderEventData>()
                .ForMember(x => x.CarCode, x => x.MapFrom(z => z.User.Car.Code))
                .ForMember(x => x.DestinationAddress, x => x.MapFrom(z => z.Order.TransportLinkage.DestinationPlaceName))
                .ForMember(x => x.OriginAddress, x => x.MapFrom(z => z.Order.TransportLinkage.StartingPlaceName))
                .ForMember(x => x.OrderId, x => x.MapFrom(z => z.Order.OrderId))
                .ForMember(x => x.OrderNumber, x => x.MapFrom(z => z.Order.OrderNumber))
                .ForMember(x => x.Quote, x => x.MapFrom(z => z.Price))
                .ForMember(x => x.DriverId, x => x.MapFrom(z => z.UserId))
                .ForMember(x => x.DriverPhone, x => x.MapFrom(z => z.User.PhoneNumber))
                .ForMember(x => x.QuoteUnit, x => x.MapFrom(z => OrderController.PriceUnitMapping[z.Unit]))
                .ForMember(x => x.DispatcherId, x => x.MapFrom(z => z.Order.ContactChatId));

            Mapper.CreateMap<Sino.CapacityCloud.WebApi.UserInfo, Repositories.Models.ApplicationUser>()
                .ForMember(x => x.AvatarKey, x => x.MapFrom(z => z.HeadImage))
                .ForMember(x => x.Car, x => x.MapFrom(z => z));

            Mapper.CreateMap<Sino.CapacityCloud.WebApi.UserInfo, Repositories.Models.Car>()
               .ForMember(x => x.Length, x => x.MapFrom(z => z.CarLength))
               .ForMember(x => x.AssignmentOfLoad, x => x.MapFrom(z => z.AuthorizedLoad))
               .ForMember(x => x.Type, x => x.MapFrom(z => z.CarType));

            Mapper.CreateMap<ApplicationUser, UserBasicInfo>()
                .ForMember(x => x.UserId, x => x.MapFrom(z => z.Id))
                .ForMember(x => x.Address, x => x.MapFrom(z => z.Address))
                .ForMember(x => x.AuthorizedLoad, x => x.MapFrom(z => z.Car.AssignmentOfLoad))
                .ForMember(x => x.AvatarUri, x => x.MapFrom(z => z.AvatarKey))
                .ForMember(x => x.CarriageType, x => x.MapFrom(z => z.CarriageType))
                .ForMember(x => x.CertificateImageKey, x => x.MapFrom(z => z.CertificatePhotoKey))
                .ForMember(x => x.DrivingLicenseImageKey, x => x.MapFrom(z => z.DrivingLicenseKey))
                .ForMember(x => x.IdentityCardImageKey, x => x.MapFrom(z => z.IdentityCardKey))
                .ForMember(x => x.Point, x => x.MapFrom(z => z.Points.Average(y => y.TotalPoints)))
                .ForMember(x => x.Profile, x => x.MapFrom(z => z.Profile))
                .ForMember(x => x.RealName, x => x.MapFrom(z => z.RealName))
                .ForMember(x => x.RealNameState, x => x.MapFrom(z => z.GetRealNameInfo().State))
                .ForMember(x => x.UserId, x => x.MapFrom(z => z.Id))
                .ForMember(x => x.UserName, x => x.MapFrom(z => z.UserName))
                .ForMember(x => x.CommonLines, x => x.MapFrom(z => z.CommonTransportLinkages))
                .ForMember(x => x.BankCards, x => x.MapFrom(z => z.BankCards));

            Mapper.CreateMap<ApplicationUser, UserDetails>()
                .ForMember(x => x.UserId, x => x.MapFrom(z => z.Id))
                .ForMember(x => x.Address, x => x.MapFrom(z => z.Address))
                .ForMember(x => x.AuthorizedLoad, x => x.MapFrom(z => z.Car.AssignmentOfLoad))
                .ForMember(x => x.AvatarUri, x => x.MapFrom(z => z.AvatarKey))
                .ForMember(x => x.CarriageType, x => x.MapFrom(z => z.CarriageType))
                .ForMember(x => x.CertificateImageKey, x => x.MapFrom(z => z.CertificatePhotoKey))
                .ForMember(x => x.DrivingLicenseImageKey, x => x.MapFrom(z => z.DrivingLicenseKey))
                .ForMember(x => x.IdentityCardImageKey, x => x.MapFrom(z => z.IdentityCardKey))
                .ForMember(x => x.Point, x => x.MapFrom(z => z.Points.Average(y => y.TotalPoints)))
                .ForMember(x => x.Profile, x => x.MapFrom(z => z.Profile))
                .ForMember(x => x.RealName, x => x.MapFrom(z => z.RealName))
                .ForMember(x => x.RealNameState, x => x.MapFrom(z => z.GetRealNameInfo().State))
                .ForMember(x => x.UserId, x => x.MapFrom(z => z.Id))
                .ForMember(x => x.UserName, x => x.MapFrom(z => z.UserName))
                .ForMember(x => x.CommonLines, x => x.MapFrom(z => z.CommonTransportLinkages))
                .ForMember(x => x.BankCards, x => x.MapFrom(z => z.BankCards))
                .ForMember(x => x.CanTargeted, x => x.MapFrom(z => z.Car.CanTargeted))
                .ForMember(x => x.CarId, x => x.MapFrom(z => z.Car.CarId))
                .ForMember(x => x.Code, x => x.MapFrom(z => z.Car.Code))
                .ForMember(x => x.Committer, x => x.MapFrom(z => z.Car.AddingCommit.Committer))
                .ForMember(x => x.CreationTime, x => x.MapFrom(z => z.Car.Date))
                .ForMember(x => x.IsEnabled, x => x.MapFrom(z => !z.LockoutEnabled))
                .ForMember(x => x.Length, x => x.MapFrom(z => z.Car.Length))
                .ForMember(x => x.Origin, x => x.MapFrom(z => z.Car.Origin))
                .ForMember(x => x.TrailerLicenseKey, x => x.MapFrom(z => z.Car.TrailerLicenseKey))
                .ForMember(x => x.Type, x => x.MapFrom(z => z.Car.Type))
                .ForMember(x => x.VehicleLicenseKey, x => x.MapFrom(z => z.Car.VehicleLicenseKey))
                .ForMember(x => x.VehiclePhotoKey, x => x.MapFrom(z => z.Car.VehiclePhotoKey));

            Mapper.CreateMap<CarAddApplicationEventData, CarDetails>()
                .ForMember(x => x.Id, x => x.MapFrom(z => default(string)))
                .ForMember(x => x.IsEnabled, x => x.MapFrom(z => true))
                .ForMember(x => x.Length, x => x.MapFrom(z => z.Length))
                .ForMember(x => x.Origin, x => x.MapFrom(z => z.Source))
                .ForMember(x => x.Type, x => x.MapFrom(z => z.Type))
                .ForMember(x => x.AuthorizedLoad, x => x.MapFrom(z => z.AssignmentOfLoad))
                .ForMember(x => x.Committer, x => x.MapFrom(z => z.Committer))
                .ForMember(x => x.CreationTime, x => x.MapFrom(z => DateTime.Now));

            Mapper.CreateMap<Repositories.Models.BankCard, BankCard>()
                .ForMember(x => x.Id, x => x.MapFrom(z => z.BankCardId))
                .ForMember(x => x.Holder, x => x.MapFrom(z => z.Holder))
                .ForMember(x => x.Number, x => x.MapFrom(z => z.Number))
                .ForMember(x => x.OperationTime, x => x.MapFrom(z => DateTime.Now))
                .ForMember(x => x.BankImage, x => x.MapFrom(z => SystemUtils.Try(() => z.Bank.LogoKey, null)))
                .ForMember(x => x.BankName, x => x.MapFrom(z => SystemUtils.Try(() => z.Bank.Name, null)));

            Mapper.CreateMap<Car, CarDetails>()
               .ForMember(x => x.Id, x => x.MapFrom(z => z.CarId))
               .ForMember(x => x.IsEnabled, x => x.MapFrom(z => z.IsEnabled))
               .ForMember(x => x.Length, x => x.MapFrom(z => z.Length))
               .ForMember(x => x.Origin, x => x.MapFrom(z => z.Origin))
               .ForMember(x => x.Type, x => x.MapFrom(z => z.Type))
               .ForMember(x => x.Committer, x => x.MapFrom(z => z.AddingCommit.Committer))
               .ForMember(x => x.CreationTime, x => x.MapFrom(z => z.Date))
               .ForMember(x => x.TrailerLicenseKey, x => x.MapFrom(z => z.TrailerLicenseCertificationStatus == CertificationStatus.Passed ? z.TrailerLicenseKey : ""))
               .ForMember(x => x.VehicleLicenseKey, x => x.MapFrom(z => z.CertificationStatus == CertificationStatus.Passed ? z.VehicleLicenseKey : ""))
               .ForMember(x => x.VehiclePhotoKey, x => x.MapFrom(z => z.VehiclePhotoCertificationStatus == CertificationStatus.Passed ? z.VehiclePhotoKey : ""))
               .ForMember(x => x.Users, x => x.MapFrom(z => z.Users));

            Mapper.CreateMap<Car, CarAddEventData>()
                .ForMember(x => x.CarLength, x => x.MapFrom(z => z.Length))
                .ForMember(x => x.CarSource, x => x.MapFrom(z => z.Origin))
                .ForMember(x => x.VehicleType, x => x.MapFrom(z => z.Type))
                .ForMember(x => x.Bearing, x => x.MapFrom(z => z.AssignmentOfLoad))
                .ForMember(x => x.CarCode, x => x.MapFrom(z => z.Code))
                .ForMember(x => x.CarId, x => x.MapFrom(z => z.CarId))
                .ForMember(x => x.CarriageType, x => x.MapFrom(z => z.CarriageType));

            Mapper.CreateMap<CarAddingModerationCommit, CarAddApplicationEventData>()
                .ForMember(x => x.CarAddingModerationCommitId, x => x.MapFrom(z => z.CarAddingModerationCommitId))
                .ForMember(x => x.Committer, x => x.MapFrom(z => z.Committer))
                .ForMember(x => x.CarCode, x => x.MapFrom(z => z.CarCode))
                .ForMember(x => x.Type, x => x.MapFrom(z => z.Type))
                .ForMember(x => x.CarriageType, x => x.MapFrom(z => z.CarriageType))
                .ForMember(x => x.Length, x => x.MapFrom(z => z.Length))
                .ForMember(x => x.AssignmentOfLoad, x => x.MapFrom(z => z.AssignmentOfLoad))
                .ForMember(x => x.Source, x => x.MapFrom(z => z.Source))
                .ForMember(x => x.Address, x => x.MapFrom(z => z.Address))
                .ForMember(x => x.Driver, x => x.MapFrom(z => z.Driver))
                .ForMember(x => x.PhoneNumber, x => x.MapFrom(z => z.PhoneNumber))
                .ForMember(x => x.ApplicationTime, x => x.MapFrom(z => z.ApplicationTime));
        }

    }

}